from django.db import models
from django import forms
from django.conf import settings
from customer.models import CustomerModel
from product.models import ProductModel as Product

import datetime

# Create your models here.

class OrderModel(models.Model):
    orderid = models.CharField(max_length=24, blank=False, null=False)
    customer = models.ForeignKey(CustomerModel, on_delete=models.CASCADE, blank=False, null=False, related_name='rn_Order_Customer')

    deposit = models.DecimalField(max_digits=10, decimal_places=2, blank=False, null=False)
    price = models.DecimalField(max_digits=10, decimal_places=2, blank=False, null=False)
    effectdays = models.PositiveSmallIntegerField(blank=True, null=False)
    balance = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=False)
    comment = models.CharField(max_length=100, blank=True, null=True)
    orderdate = models.DateField(blank=False, null=False)

    class Meta:
        ordering = ('-id',)
    
    def __str__(self):
        return 'order:{},{},{},{},{},{}'.format(self.orderid, self.customer, self.deposit, self.price, self.effectdays, self.balance)
    
    def get_effect_days(self):
        now = datetime.date.today()
        delta = now - self.orderdate
        return delta.days + 1

    def get_balance(self):
        balance = self.deposit - self.price * self.get_effect_days() 
        return balance

    def update_data_per_days(self):
        now = datetime.date.today()
        delta = now - self.orderdate 
        self.effectdays = delta.days + 1 
        self.balance = self.deposit - self.price * self.effectdays
        self.save()

class OrderContentModel(models.Model):
    order = models.ForeignKey(OrderModel, on_delete=models.CASCADE, blank=True, null=True, related_name='rn_OrderContent_Order')
    productCategoryId = models.CharField(max_length=10, blank=True, null=True)
    productId = models.IntegerField(max_length=10, blank=True, null=True)
    productCount = models.IntegerField(max_length=10, blank=True, null=True)

    def __str__(self):
        return 'orderContent:{},{},{}'.format(self.order.id, self.productId, self.productCount)
